package com.llcbenwu.controller;

import com.llcbenwu.config.Result;
import com.llcbenwu.config.adminFilter;
import com.llcbenwu.pojo.vo.FileInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.jni.FileInfo;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;


/**
 * @author lilinchun
 * @date 2021/10/21 0021 09:58
 */
@RestController
@Api(value = "测试",tags = "控制器二")
@RequestMapping("/security")
public class DemoController {


    @GetMapping("/test")
    @ApiOperation(value="测试", notes="测试")
//    @adminFilter
    public Result<String> tesOne(){
        return new Result<>("测试成功");
    }

    @GetMapping("/me")
    @ApiOperation(value = "当前用户信息")
    public Result<Object> getMe() {
        return new Result<>(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
    }



    @PostMapping("/user")
    @ApiOperation(value = "项目测试")
    public Result<String> getUser(){
        return new Result<>("测试成功");
    }



    private String folder = "com.llcbenwu.file";

    @PostMapping("/file")
    @ApiOperation(value = "文件上传到本项目")
    @ResponseBody
    public Result<FileInfoVO> upload() throws Exception {
        System.out.println("12465465");
        return new Result<>(new FileInfoVO(null));
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "文件下载")
    public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws Exception {

        try (InputStream inputStream = new FileInputStream(new File(folder, id ));
             OutputStream outputStream = response.getOutputStream();) {

            response.setContentType("application/x-download");
            response.addHeader("Content-Disposition", "attachment;filename=test.txt");

            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
        }

    }

}
